<?php

class Controller_BaseController extends FLEA_Controller_Action
{
	//数据表对象
	var $table;

	var $conditions=null;
	
   
    function actionList() {
		
		FLEA::loadClass('FLEA_Ajax');
		FLEA::loadClass('FLEA_Helper_Pager');

		$start = isset($_POST['start']) ? (int)$_POST['start'] : 0;
		$limit = isset($_POST['limit']) ? (int)$_POST['limit'] : 20;
		$sort = isset($_POST['sort']) ? $_POST['sort'] : '';
		$dir = isset($_POST['dir']) ? $_POST['dir'] : '';
		if ($sort != "" && $dir != "")
			$sortby=$sort.' '.$dir;
		else $sortby=null;
		$page=$start/$limit;
        $pager =& new FLEA_Helper_Pager($this->table, $page,$limit,$this->conditions,$sortby);
        
		$arr=$pager->findAll();

		echo '{totalCount:'.$pager->totalCount.',Result:'.json_encode($arr).'}';

    }
    function actionModify() {
		
		__TRY();
		    
		    $data = $this->_prepareData($this->table->meta); 
			if(isset($data['password']))unset($data['password']);
			if($this->table->update($data))
				echo "{success:true,data:'成功保存'}";
			 

		$ex = __CATCH();//捕获异常
        if (__IS_EXCEPTION($ex)) {
            $out = "{success:false,data:'保存失败'}";
			echo $out;
        }
    }
     function actionDelete() {

		if(!isset($_POST[$this->table->primaryKey])) {
			echo "{success:false,data:'删除失败'}";
			
			
		}else{
			__TRY();
				$result = $this->table->removeByPkv($_POST[$this->table->primaryKey]);
				if($result) 
					echo "{success:true,data:'成功删除'}";
				 
				return;
			$ex = __CATCH();
			if (__IS_EXCEPTION($ex)) {
				echo  "{success:false,data:'删除失败'}";
				 
				
			}
				
		}
    } 
     function actionAdd() {
		
		__TRY(); 
		    $data = $this->_prepareData($this->table->meta); 
			$id = $this->table->create($data);
			if( false !== $id)
				echo  "{success:true,data:'".json_encode($this->table->findAllByPkvs(array($id)))."'}";
			 

		$ex = __CATCH();
        if (__IS_EXCEPTION($ex)) {
            echo "{success:false,data:'添加失败'}";
			
        }		
	 }

	 function actionChangePassword(){
	   if(!isset($_POST[$this->table->primaryKey])) {
			echo "{success:false,data:'修改失败'}";
			
			
		}else{
			__TRY();
				$data=array();
				$data[$this->table->primaryKey]=(int)$_POST[$this->table->primaryKey];
				$data['password']=crypt($_POST['password']);
				
				if($this->table->update($data)) 
					echo "{success:true,data:'修改密码成功'}";
				 
				return;
			$ex = __CATCH();
			if (__IS_EXCEPTION($ex)) {
				echo  "{success:false,data:'密码修改失败'}";
				 
				
			}
				
		}
   }

	  /**
     * 根据数据表的元数据返回一个数组，这个数组包含所有需要初始化的数据，可以用于界面显示
     *
     * @param array $meta
     *
     * @return array
     */
    function _prepareData(& $meta) {
        $data = array();
		
        foreach ($meta as $m) {
            if (isset($_POST[$m['name']])&&trim($_POST[$m['name']])!='') {
				if($m['primaryKey']!=1)
					if($m['type']!='tinyint'){
					    if($m['name']=='password')
							$data[$m['name']] = crypt($_POST[$m['name']]);
						else
							$data[$m['name']] =strip_tags($_POST[$m['name']]);
					}
					else
						$data[$m['name']] = '1';
				else
					$data[$m['name']]=(int)$_POST[$m['name']];
            } else {
                if (isset($m['defaultValue'])) {
                    $data[$m['name']] = $m['defaultValue'];
                } else {
                    $data[$m['name']] = null;
                }
            }
        }
        return $data;
    }
}    
?>